package mysql

import (
	"gorm.io/gorm"
	"hm-fresh-api/freshrpc/freshrpc"
)

type Fresh struct {
	gorm.Model
	Title   string  `gorm:"type:varchar(30)"`
	Price   float64 `gorm:"type:decimal(10,2)"`
	ClassId int64   `gorm:"type:int"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fresh) FindFreshByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(f).Error
}

func FreshList(db *gorm.DB) ([]*freshrpc.FreshList, error) {
	var list []*freshrpc.FreshList
	err2 := db.Debug().Model(&Fresh{}).
		Select("freshes.id as fresh_id",
			"title",
			"price",
			"class_id").
		Find(&list).Error
	return list, err2
}
